package com.groupseven.happycampus.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.groupseven.happycampus.common.bo.CardBo;
import com.groupseven.happycampus.common.instener.WebCardListener;
import com.groupseven.happycampus.common.pojo.Card;
import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @BeLongsProject: happycampus
 * @BeLongsPackage: com.groupseven.happycampus.controller
 * @Author: WRH
 * @CreateTime: 2022-07-26 15:22
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/card")
public class CardController {
    @Autowired
    private CardService cardService;
    @Autowired
    private WebCardListener webCardListener;


    @RequestMapping("/insertCard")
    public R insertCard(@RequestParam Map<String,Object> params) {
        Boolean flag = cardService.insertCard(params);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @RequestMapping("/deleteCard")
    public R deleteCard(@RequestParam Map<String,Object> params) {
        Boolean flag = cardService.deleteCard(params);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @RequestMapping("/cardList")
    public LayuiPageVo<Card> cardList(@RequestParam Map<String,Object> params) {

        return cardService.cardList(params);
    }

    @RequestMapping("/updateCard")
    public R updateCard(@RequestParam Map<String,Object> params) {
        Boolean flag = cardService.updateCard(params);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @RequestMapping("/deleteCardsByIds")
    public R deleteCardsByIds(@RequestParam String ids){
        boolean flag = cardService.deleteCardsByIds(ids);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @RequestMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        // 避免遇见请求报错
        if(file!=null){

            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), CardBo.class, webCardListener);
            // 得到 sheet 对象
            ExcelReaderSheetBuilder sheet = read.sheet();
            // 读取sheet
            sheet.doRead();

        }
        return R.ok();
    }

    @RequestMapping("/export")
    public void export(Card card, HttpServletResponse response) throws IOException {
        //下载excel 的 响应数据格式
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("订单信息导出", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, CardBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();
        //准备满足条件的集合数据
        List<CardBo> orders = cardService.cardExcelData(card);
        // 把这个集合数据 写到 输出流
        sheet.doWrite(orders);
    }

    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //读取服务器上的模板文件 写到浏览器
        //得到模板文件的路径
        String realPath = request.getServletContext().getRealPath("/template/开卡信息模板.xlsx");

        //根据文件路径 创建File 对象
        File file = new File(realPath);

        // 把File  对象转为 字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("开卡信息模板", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();
        // 把字节数组 输出到浏览器
        outputStream.write(bytes);

        outputStream.close();
    }
}
